package silvercrux.main;

import org.antlr.runtime.ANTLRFileStream;
import org.antlr.runtime.ANTLRInputStream;
import org.antlr.v4.gui.SystemFontMetrics;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import silvercrux.cmm.*;
import org.antlr.v4.gui.Trees;
import silvercrux.runtime.Array;
import silvercrux.runtime.RuntimeVirtualMachine;
import sun.java2d.cmm.kcms.CMM;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.lang.String;
import java.lang.invoke.SwitchPoint;
import java.util.Scanner;

/**
 * Created by SilverCrux on 2016/11/15.
 */
public class Program {
    public static void main(String args[])
    {
        try {
            int i;
//            Scanner s = new Scanner(System.in);
//            System.out.println("请输入编号选择测试文件:");
//            System.out.println("1: 变量声明");
//            System.out.println("2: 一般变量赋值");
//            System.out.println("3: 数组");
//            System.out.println("4: 算数运算");
//            System.out.println("5: IF-ELSE");
//            System.out.println("6: WHILE");
//            System.out.println("7: IF-ELSE WHILE FOR");
//            System.out.println("8: 阶乘");
//            System.out.println("9: 排序升级版-函数调用");
//            i = s.nextInt();
//            while(!(i<=9&&i>=1))
//            {
//                System.out.println("请重新选择:");
//            }
            i = 8;
            String str;
            str = ".\\Test\\test" + i + ".cmm";
            str = readFileByLines(str);
            //str = "int a[3] = {1,2,3};\nint i = 5;\nwhile(i!=2) {i=i-1;write(i);write(a[0]);a[0]=i;}";
            org.antlr.v4.runtime.ANTLRInputStream input = new org.antlr.v4.runtime.ANTLRInputStream(str);
            cmmLexer lexer = new cmmLexer(input);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            cmmParser parser = new cmmParser(tokens);
            ParseTree tree = parser.prog();
            //cmmBaseListener listener = new cmmBaseListener();
            //ParseTreeWalker walker = new ParseTreeWalker();
            //walker.walk(listener,tree);
            //Trees.inspect(tree,parser);
            SCCVisitor visitor = new SCCVisitor();
            visitor.visit(tree);
            //System.out.println(((Array) RuntimeVirtualMachine.getIdMap().get("a")).array[0].value);
        }catch(Exception e)
        {
            //System.out.println(e.printStackTrace());
            e.printStackTrace();
        }
    }

    /**
     * 以行为单位读取文件，常用于读面向行的格式化文件
     */
    public static String readFileByLines(String fileName) throws IOException {
        File file = new File(fileName);
        BufferedReader reader = null;
        String tempString = "";
        try {
            System.out.println("以行为单位读取文件内容，一次读一整行：");
            reader = new BufferedReader(new FileReader(file));
            String tmp;
            int line = 1;
            // 一次读入一行，直到读入null为文件结束
            while ((tmp = reader.readLine()) != null) {
                // 显示行号
                //System.out.println("line " + line + ": " + tempString);
                tempString += tmp + "\n";
                line++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        return tempString;
    }
}
